package com.ruoyi.web.controller.mail;

import cn.hutool.core.io.IoUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
@Component
public class TemplateService {
    public String renderTemplate(String templatePath, Map<String, String> params) {
        ClassPathResource resource = new ClassPathResource(templatePath);
        String template = null;
        try {
            template = IoUtil.read(resource.getInputStream(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (Map.Entry<String, String> entry : params.entrySet()) {
            template = template.replace("{" + entry.getKey() + "}", entry.getValue());
        }
        return template;
    }
}
